package com.cloudcc.mobile.adapter;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.AddImageInfo;
import com.cloudcc.mobile.util.photo.Constants;
import com.cloudcc.mobile.util.photo.Util;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class ImageGridAdapter extends BaseAdapter {
	private Context context;
	private List<AddImageInfo> list;
	private boolean isInDeleteMode = false;
	private OndeleteListener ondeleteListener;
	private OnAddImageListener onAddImageListener;
	private OnShowImageListener onShowImageListener;
	private ImageLoadingListener animateFirstListener = new Util.AnimateFirstDisplayListener();
	public ImageGridAdapter(Context context, List<AddImageInfo> list) {
		this.context = context;
		this.list = list;
	}

	public void changeData(List<AddImageInfo> list){
		this.list=list;
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		return 2 + list.size();
	}

	public void setDeleteModel(boolean isInDeleteMode) {
		this.isInDeleteMode = isInDeleteMode;
		this.notifyDataSetChanged();
	}

	public void setOndeleteListener(OndeleteListener ondeleteListener){
		this.ondeleteListener=ondeleteListener;
	}
	public void setOnAddImageListener(OnAddImageListener onAddImageListener){
		this.onAddImageListener=onAddImageListener;
	}
	public void setOnShowImageListener(OnShowImageListener onShowImageListener){
		this.onShowImageListener=onShowImageListener;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		// TODO Auto-generated method stub
		if (view == null)
			view = LayoutInflater.from(context).inflate(
					R.layout.image_grid_item, null);
		ImageView image = (ImageView) view.findViewById(R.id.image_grid_item_img);
		ImageView delete = (ImageView) view.findViewById(R.id.badge_delete);
		FrameLayout photo = (FrameLayout) view.findViewById(R.id.addphoto);

		//设置滑动图片的宽与高  这里的RelativeLayout是这个布局的父控件是什么布局
		LinearLayout.LayoutParams imagebtn_params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		imagebtn_params.height =AppContext.screenW/4-15;
		imagebtn_params.width = AppContext.screenW/4-15;
		photo.setLayoutParams(imagebtn_params);
		if (this.isInDeleteMode)
			delete.setVisibility(View.VISIBLE);
		else
			delete.setVisibility(View.GONE);
		delete.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramAnonymousView) {
				ondeleteListener.delete(position);
			}
		});
		if (position == -1 + getCount()) {
			image.setImageResource(R.drawable.smiley_minus_btn);
			delete.setVisibility(View.GONE);
			if(isInDeleteMode==true)
				view.setVisibility(View.GONE);
			else
				view.setVisibility(View.VISIBLE);
			image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					isInDeleteMode=true;
					ImageGridAdapter.this.notifyDataSetChanged();
				}
			});
		}else if (position == -2 + getCount()) {
			image.setImageResource(R.drawable.smiley_add_btn);
			delete.setVisibility(View.GONE);
			if(isInDeleteMode==true)
				view.setVisibility(View.GONE);
			else
				view.setVisibility(View.VISIBLE);
			image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					isInDeleteMode=false;
					ImageGridAdapter.this.notifyDataSetChanged();
					onAddImageListener.add();

				}
			});
		}else{
			AddImageInfo localImageInfo = list.get(position);
			//			image.setImageBitmap(ImageUtils.getimage(localImageInfo.getImagePath()));
			/**
			 * 第一种
			 */
			//			image.setImageBitmap(ImageUtils.zoomBitmap(ImageUtils.getimage(localImageInfo.getImagePath()), 50));
			/**
			 * 第二种
			 */
			//			//利用Bitmap位图对象生成缩略图  
			//			Bitmap bitmap = ImageUtils.getImageThumbnail(localImageInfo.getImagePath(), 50, 50);
			//			image.setImageBitmap(bitmap);
			/**
			 * 第三种
			 */
			ImageLoader.getInstance().displayImage("file://"+localImageInfo.getImagePath(), image,Constants.qiandao_image,animateFirstListener);
			image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onShowImageListener.show(position);
				}
			});
		}
		return view;
	}

	@Override
	public AddImageInfo getItem(int position) {
		return list.get(position);
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	public interface OndeleteListener{
		public void delete(int posion);
	}
	public interface OnAddImageListener{
		public void add();
	}
	public interface OnShowImageListener{
		public void show(int posion);
	}

	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
